package  
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import caurina.transitions.Tweener;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.filters.BlurFilter;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.text.TextFormat;
	import ginger.ui.simple.SimpleBaseComponent;
	import ginger.ui.simple.SimpleBrightnessButton;
	import ginger.ui.simple.SimpleLabel;
	import ginger.utils.DisplayUtil;
	import uiLib.BoxBgBmpdata;
	import uiLib.PraiseBmpData;
	import UILib.ShareBox;
	/**
	 * ...
	 * @author Ning
	 */
	public class PhotoItem extends SimpleBaseComponent
	{
		private var _bgBmp:Bitmap;
		private var _praiseBtn:SimpleBrightnessButton;
		private var _praiseLabel:SimpleLabel;
		private var _praiseLabelBmp:Bitmap;
		private var _label:SimpleLabel;
		private var _praiseBox:Sprite;
		private var _titleLabel:SimpleLabel;
		private var _titleLabelBmp:Bitmap;
		private var _shareBox:ShareBox;
		private var _codeLabel:SimpleLabel;
		private var _codeBmp:Bitmap;
		
		public function set label(value:String):void {
			_label.text = value;
		}
		public function get label():String {
			return _label.text;
		}
		
		private var _data:PhotoVO;
		public function get data():PhotoVO {
			return _data;
		}
		
		private var _photoBox:Sprite;
		private var _loader:Loader;
		private var _photoMask:Shape;
		private var _curBmp:Bitmap;
		private var _shareBtns:Array;
		
		//正在载入的标签
		private var _loadingFlag:Boolean = false;
		//有另一个载入
		private var _hadOtherLoad:Boolean = false;
		
		
		//当前索引号
		private var _index:int;
		public function get index():int {
			return _index;
		}
		public function PhotoItem() 
		{
			
		}
		override protected function init():void 
		{
			_loader = new Loader();
			_shareBtns=['btn1','btn2','btn3','btn4']
			super.init();
			drawLayout();
		}
		override protected function setupListener():void 
		{
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			_praiseBox.addEventListener(MouseEvent.CLICK, onPraiseClick);
			
			for (var i:int = 0; i < _shareBtns.length; i++ )
			{
				var btn:SimpleButton = _shareBox[_shareBtns[i]];
				btn.addEventListener(MouseEvent.CLICK, onShare);
			}
			
			
			super.setupListener();
		}
		override protected function configUI():void 
		{
			graphics.clear();
			graphics.beginFill(0xffffff);
			graphics.drawRoundRect(0, 0, 356, 295,20,20);
			graphics.endFill();
			
			this.scaleX = this.scaleY = AppGlobal.SCALE_ARRAY[0];
			this.alpha = 0;
			
			_label = new SimpleLabel();
		//	addChild(_label);
		
		    _praiseBox = new Sprite();
			addChild(_praiseBox);
			_praiseBox.buttonMode = true;
		
		    _praiseLabel = new SimpleLabel();
			_praiseLabel.defaultTextFormat = new TextFormat('微软雅黑', 20, 0x00b4ff, true);
			_praiseLabel.filters = [new BlurFilter(1.1, 1.1)]
			
			
			
			_photoBox = new Sprite();
			addChild(_photoBox);
			
			_bgBmp = new Bitmap(new BoxBgBmpdata);
			_photoBox.addChild(_bgBmp);
			
			
			_photoMask = new Shape();
			addChild(_photoMask);
			
			_photoMask.graphics.clear();
			_photoMask.graphics.beginFill(0);
			_photoMask.graphics.drawRoundRect(0, 0, 335, 230, 20, 20);
			_photoMask.graphics.endFill();
			_photoBox.mask = _photoMask;
			
			_praiseBtn = new SimpleBrightnessButton(new Bitmap(new PraiseBmpData));
			_praiseBox.addChild(_praiseBtn);
			
			_titleLabel = new SimpleLabel();
			_titleLabel.defaultTextFormat = new TextFormat('微软雅黑', 14, 0xcc2d1c);
			_titleLabel.filters = [new BlurFilter(1.1, 1.1)];
			
			_codeLabel = new SimpleLabel();
			_codeLabel.defaultTextFormat = new TextFormat('微软雅黑', 12, 0xcc2d1c);
			_codeLabel.filters = [new BlurFilter(1.1, 1.1)];
			
			_shareBox = new ShareBox();
			addChild(_shareBox);
			
		
			this.filters=[new DropShadowFilter(4,90,0,.3)]
		}
		override protected function drawLayout():void 
		{
			this.x = AppGlobal.POS_X_ARRAY[0];
			this.y = AppGlobal.POS_Y_ARRAY[0];
			_photoBox.x = 10;
			_photoBox.y = 10;
			_photoMask.x = 10;
			_photoMask.y = 10;
			
			_praiseBtn.x = 15;
			_praiseBtn.y = 265;
			
			_shareBox.x = 230;
			_shareBox.y = 274;
			
			//_praiseLabel.y = 255;
			//_praiseLabel.x = 38;
			
		}
		/**
		 * 
		 * @param	index   索引号
		 * @param	dirBool   进退方向标识
		 */
		public function showAt(index:int, dirBool:Boolean ):void
		{
			_index = index;
			if (index == 17)
			    this.mouseChildren = this.mouseEnabled = false;
			else 
			    this.mouseChildren = this.mouseEnabled = true;
			var alpha:Number = 1;
			var posX:Number = AppGlobal.POS_X_ARRAY[index];
			var posY:Number = AppGlobal.POS_Y_ARRAY[index];
			var scale:Number = AppGlobal.SCALE_ARRAY[index];
			
			if (index == AppGlobal.POS_X_ARRAY.length - 1) {
				alpha = 0;
			}
			
			if (!dirBool) {
				if (index == AppGlobal.POS_X_ARRAY.length - 2) {
					this.alpha = 0;
					this.x = AppGlobal.POS_X_ARRAY[AppGlobal.POS_X_ARRAY.length-1];
					this.y = AppGlobal.POS_Y_ARRAY[AppGlobal.POS_X_ARRAY.length-1];
					this.scaleX = this.scaleY = AppGlobal.SCALE_ARRAY[AppGlobal.POS_X_ARRAY.length-1];
					if(this.parent)
						this.parent.setChildIndex(this, this.parent.numChildren-1);
			    }
				if (index == 0) {
					alpha = 0;
				}
			
			}
			else {
				if (index == 1) {
					this.x = AppGlobal.POS_X_ARRAY[0];
					this.y = AppGlobal.POS_Y_ARRAY[0];
					this.scaleX = this.scaleY = AppGlobal.SCALE_ARRAY[0];
					this.alpha = 0;
					if(this.parent)
						this.parent.setChildIndex(this, 0);
			   }
			}
			Tweener.addTween(this, { time:2, x:posX, y:posY, scaleX:scale, scaleY:scale,alpha:alpha});
		}
		public function next():void
		{
			var index:int = _index + 1;
			index = index > (AppGlobal.POS_X_ARRAY.length - 1)?1:index;
		    showAt(index,true)
		}
		public function prev():void
		{
			var index:int = _index - 1;
			index = index < 0?AppGlobal.POS_X_ARRAY.length - 2:index;
		    showAt(index,false)
		}
		public function setData(data:PhotoVO):void
		{
			_data = data;
			this.label = String(_data.ID);
			
			setPraise(_data.PRAISE);
			
			_titleLabel.text = String(_data.TITLE);
			if (_titleLabelBmp && contains(_titleLabelBmp)) {
				_titleLabelBmp.bitmapData.dispose();
			    removeChild(_titleLabelBmp);
			}
			_titleLabelBmp = DisplayUtil.getBitmapByDisplay(_titleLabel);
			_titleLabelBmp.smoothing = true;
			addChild(_titleLabelBmp);
			_titleLabelBmp.x = 345-_titleLabelBmp.width;
			_titleLabelBmp.y = 247;
			
			if (_codeBmp && contains(_codeBmp)) {
				_codeBmp.bitmapData.dispose();
			    removeChild(_codeBmp);
			}
			
			_codeLabel.text = data.CODE == null?'':data.CODE;
			_codeBmp = DisplayUtil.getBitmapByDisplay(_codeLabel);
			_codeBmp.smoothing = true;
			addChild(_codeBmp);
			_codeBmp.x = 13;
			_codeBmp.y = 247;
			
			
			
			trace(data.URL+' '+data.CODE+' '+data.bitmap)
			if (_curBmp) {
				if (_photoBox.contains(_curBmp))
				    _photoBox.removeChild(_curBmp);
			}
			if (data.bitmap) {
                _curBmp = data.bitmap;
				_photoBox.addChild(_curBmp);
			}
			else {
				if (_loadingFlag)
				    _hadOtherLoad = true;
				else 
				    _hadOtherLoad = false;
					
				_loadingFlag = true;
			    _loader.load(new URLRequest(data.URL));
			}
		}
		public function setPraise(value:Number):void
		{
			_data.PRAISE = value;
			_praiseLabel.text = String(_data.PRAISE);
			if (_praiseLabelBmp && _praiseBox.contains(_praiseLabelBmp)) {
				_praiseLabelBmp.bitmapData.dispose();
			    _praiseBox.removeChild(_praiseLabelBmp);
			}
			_praiseLabelBmp = DisplayUtil.getBitmapByDisplay(_praiseLabel);
			_praiseLabelBmp.smoothing = true;
			_praiseBox.addChild(_praiseLabelBmp);
			_praiseLabelBmp.x = 38;
			_praiseLabelBmp.y = 257;
		}
		private function onComplete(evt:Event):void
		{
			_curBmp = evt.target.content as Bitmap;
			_photoBox.addChild(_curBmp);
			_curBmp.smoothing = true;
			_curBmp.alpha = 0;
			Tweener.addTween(_curBmp, { time:.5, alpha:1 } );
			
			_loadingFlag = false;
			if(!_hadOtherLoad)
			    _data.bitmap = _curBmp;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		private function onIOError(evt:Event):void
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
		private function onPraiseClick(evt:MouseEvent):void
		{
			if (ExternalInterface.available) {
				ExternalInterface.call('onPraise', _data.ID);
			}
		}
		private function onShare(evt:MouseEvent):void
		{
			var btn:SimpleButton = evt.target as SimpleButton;
			var index:int = _shareBtns.indexOf(btn.name);
			if (ExternalInterface.available) {
				ExternalInterface.call('onShare', _data.ID, index);
			}
		}
	}
}